/*
* Copyright (C) 2012 Manuel Pérez
*
* This file is part of OpenSudoku.
*
* OpenSudoku is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSudoku is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenSudoku. If not, see <http://www.gnu.org/licenses/>.
*
*/
package igraf.diagnostikoak;

import igraf.ItzuliInterfazea;
import igraf.Panela;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.text.MaskFormatter;

import dBasea.Bidaiariak;
import dBasea.Diagnostikoa;
import dBasea.Medikua;

public class DiagnostikoaSartu extends Panela{
	private static final long serialVersionUID = 7429862095600900729L;
	private JTextField ize,abi,lesi;
	JTextField klini;
	JComboBox zenba;
	private JButton ok, ezeztatu,aurkitu;
	private JTextField istri;
	private JLabel klinika;
	private JComponent lesioa;
	private JComponent abizenak;
	private JComponent izena;
	private JLabel zenbakiaAbo;
	private Component istripu;
	private Diagnostikoa diag=new Diagnostikoa();
	private Medikua medi=new Medikua();
	private Bidaiariak bida=new Bidaiariak();
	
	public DiagnostikoaSartu(String tituloa, final ItzuliInterfazea itzuliInterfazea) {		
		super(tituloa);
		
		JPanel erdikoPanela = this.getErdikoPanela();
		erdikoPanela.setBackground(SystemColor.activeCaption);
		erdikoPanela.setLayout(new BorderLayout());
		
		
		//Erdiko panelak
		JPanel erdikoa = new JPanel();
		erdikoa.setBackground(SystemColor.activeCaption);		
		erdikoPanela.add(erdikoa, BorderLayout.CENTER);
		erdikoa.setLayout(new GridLayout(10,1));
		
		JPanel g=new JPanel();
		g.setBackground(SystemColor.activeCaption);
		erdikoa.add(g);
		
		JPanel erd = new JPanel();
		erd.setBackground(SystemColor.activeCaption);
		FlowLayout fl_erdiikkk = new FlowLayout();
		fl_erdiikkk.setHgap(15);
		fl_erdiikkk.setVgap(8);
		erd.setLayout(fl_erdiikkk);
		istripu=new JLabel("Bidaiariren NAN:");
		istripu.setFont(new Font("Arial", Font.PLAIN, 14));
		istri = new JTextField();
		istri.addKeyListener(new KeyAdapter() {// limitea NAN

			public void keyTyped(KeyEvent arg0) {
				if (istri.getText().length() >= 10) {
					arg0.consume();
				}
			}
		});
		istri.setColumns(8);
		aurkitu=new JButton("Bilatu");
		aurkitu.setIcon(new ImageIcon(DiagnostikoaSartu.class.getResource("/Argazkia/lupaaa.png")));

		erd.add(istripu);
		erd.add(istri);
		erd.add(aurkitu);
		erdikoa.add(erd);
		
		JPanel arribb = new JPanel();
		arribb.setBackground(SystemColor.activeCaption);
		FlowLayout arrib = new FlowLayout();
		arrib.setHgap(15);
		arrib.setVgap(10);
		arribb.setLayout(arrib);
		zenbakiaAbo=new JLabel("Medikuak:");
		zenbakiaAbo.setFont(new Font("Arial", Font.PLAIN, 14));
		zenba=new JComboBox();
		
		

				try {
					boxCarga();
				} catch (SQLException e) {
					JOptionPane.showMessageDialog(null,
							"Ezin izan da kargatu DB-tik mediku zerrenda",
							"Oker", JOptionPane.ERROR_MESSAGE);
					e.printStackTrace();
				}
		
		arribb.add(zenbakiaAbo);
		arribb.add(zenba);
		erdikoa.add(arribb);
		
		JPanel xx = new JPanel();
		xx.setBackground(SystemColor.activeCaption);
		FlowLayout fl_arrib = new FlowLayout();
		fl_arrib.setHgap(15);
		fl_arrib.setVgap(9);
		xx.setLayout(fl_arrib);
		izena=new JLabel("Data:");
		izena.setFont(new Font("Arial", Font.PLAIN, 14));
		try {
			ize= new JFormattedTextField(new MaskFormatter("   ##/##/####  "));
		} catch (ParseException e1) {
			JOptionPane.showMessageDialog(null,
					"Data Sartu Ondo", "Oker",
					JOptionPane.ERROR_MESSAGE);
			e1.printStackTrace();
		}
		xx.add(izena);
		xx.add(ize);
		erdikoa.add(xx);
		

		JPanel erdi = new JPanel();
		erdi.setBackground(SystemColor.activeCaption);
		FlowLayout fl_erdi = new FlowLayout();
		fl_erdi.setHgap(15);
		fl_erdi.setVgap(8);
		erdi.setLayout(fl_erdi);
		abizenak=new JLabel("Lesioak:");
		abizenak.setFont(new Font("Arial", Font.PLAIN, 14));
		abi=new JTextField(16);
		erdi.add(abizenak);
		erdi.add(abi);
		erdikoa.add(erdi);
		
		JPanel erdik = new JPanel();
		erdik.setBackground(SystemColor.activeCaption);
		FlowLayout fl_erdii = new FlowLayout();
		fl_erdii.setHgap(15);
		fl_erdii.setVgap(8);
		erdik.setLayout(fl_erdii);
		lesioa=new JLabel("Tratamendua:");
		lesioa.setFont(new Font("Arial", Font.PLAIN, 14));
		lesi=new JTextField(16);
		erdik.add(lesioa);
		erdik.add(lesi);
		erdikoa.add(erdik);
		
		JPanel erdiko = new JPanel();
		erdiko.setBackground(SystemColor.activeCaption);
		FlowLayout fl_erdiikk = new FlowLayout();
		fl_erdiikk.setHgap(15);
		fl_erdiikk.setVgap(8);
		erdiko.setLayout(fl_erdiikk);
		klinika=new JLabel("Oharrak:");
		klinika.setFont(new Font("Arial", Font.PLAIN, 14));
		klini=new JTextField(14);
		erdiko.add(klinika);
		erdiko.add(klini);
		erdikoa.add(erdiko);
		
		
		
		JPanel erdikox = new JPanel();
		erdikox.setBackground(SystemColor.activeCaption);
		FlowLayout fl_erdiiko = new FlowLayout();
		fl_erdiiko.setHgap(15);
		fl_erdiiko.setVgap(8);
		erdikox.setLayout(fl_erdiiko);

		erdikoa.add(erdikox);
		
		
		
		//beheko panela
		
		JPanel behekoa = new JPanel();
		FlowLayout flowLayout = (FlowLayout) behekoa.getLayout();
		flowLayout.setVgap(15);
		flowLayout.setHgap(110);
		behekoa.setBackground(SystemColor.activeCaption);
		ok = new JButton("Sartu diagnostikoa");
		ok.setHorizontalAlignment(SwingConstants.LEFT);
		ok.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(ok);
		ezeztatu = new JButton("Ezeztatu");
		ezeztatu.setHorizontalAlignment(SwingConstants.RIGHT);
		ezeztatu.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(ezeztatu);
		erdikoPanela.add(behekoa, BorderLayout.SOUTH);
		ezeztatu.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				itzuliInterfazea.itzuliLehenPanela();
				ezarri(false);
			}
		});
		ezarri(true);
		aurkitu.addActionListener(new ActionListener() {			

			public void actionPerformed(ActionEvent arg0) {
				String zer = istri.getText();
				if (zer.isEmpty()) {
					JOptionPane.showMessageDialog(null,
							"Sartu bidaiari bat, mesedez!", "Oker",
							JOptionPane.ERROR_MESSAGE);
				} else {
					try {
						if (bida.dagoBidaiaria(zer)) {
										ezarri(false);					}
						else {
							JOptionPane.showMessageDialog(null,
									"Ez da existitzen sartu duzuna", "Oker",
									JOptionPane.ERROR_MESSAGE);
						}
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}

		});	
		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
					if (zerbaitIdatzi()) {
						JOptionPane.showMessageDialog(null,
								"Sartu behar duzu zerbait zuriunetan\n"
										+ "Ezin da eremu hutsik egon", "Oker",
								JOptionPane.ERROR_MESSAGE);
					
					} else {
						try {
							DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
							Date da = null;
							try {
								da = formatter.parse(ize.getText());
							} catch (ParseException e) {
								JOptionPane.showMessageDialog(null, "Datan zuriuneak daude edo txarto jarrita!", "Oker", JOptionPane.ERROR_MESSAGE);				
								e.printStackTrace();
								e.printStackTrace();
							}
							long data = da.getTime();
						
							try {
								diag.diagSortu((String)zenba.getSelectedItem(),data, abi.getText(),lesi.getText(),klini.getText(), istri.getText());
							} catch (SQLException e) {
								e.printStackTrace();
							}
							JOptionPane.showMessageDialog(null,
									"Ondo sortu da Diagnostikoa", "",
									JOptionPane.INFORMATION_MESSAGE);
					
							eremuakGarbitu();
							ezarri(true);
						} catch (NumberFormatException nfe) {
							JOptionPane.showMessageDialog(null,
									"Zenbakiak sartu zenbatekoa eremuan",
									"Oker", JOptionPane.ERROR_MESSAGE);
						}
					}
				}
					
		});}
	

	




	private void boxCarga() throws SQLException {
		List<String> lista = medi.getMedikuak();
		zenba.removeAllItems();
		if (lista != null) {
			for (String hiria: lista) {
				zenba.addItem(hiria);
			}
		}		
	}



	private boolean zerbaitIdatzi() {
		boolean aurki;
		aurki=( ize.getText().isEmpty() && abi.getText().isEmpty()
				&& lesi.getText().isEmpty()&& istri.getText().isEmpty());

		return aurki;
	}

	private void ezarri(boolean balioa) {
		istri.setEnabled(balioa);		
		ize.setEnabled(!balioa);
		abi.setEnabled(!balioa);
		lesi.setEnabled(!balioa);
		klini.setEnabled(!balioa);
		
		ok.setEnabled(!balioa);
	}

	@Override
	public void eremuakGarbitu() {
		istri.setText("");
		ize.setText("");
		abi.setText("");
		lesi.setText("");
		klini.setText("");
		zenba.setSelectedIndex(0);
		
		
	}


}
